package com.hmj.excel.frame;

import com.alibaba.fastjson.JSONObject;
import com.hmj.excel.biz.ExcelPrase;
import com.hmj.excel.biz.FinanceService;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DiffActionListener implements ActionListener{
    public JTextField getFileField1() {
        return fileField1;
    }

    public void setFileField1(JTextField fileField1) {
        this.fileField1 = fileField1;
    }

    public JTextField getFileField2() {
        return fileField2;
    }

    public void setFileField2(JTextField fileField2) {
        this.fileField2 = fileField2;
    }

    public JTextField getFileField3() {
        return fileField3;
    }

    public void setFileField3(JTextField fileField3) {
        this.fileField3 = fileField3;
    }

    public JTextField getFileField4() {
        return fileField4;
    }

    public void setFileField4(JTextField fileField4) {
        this.fileField4 = fileField4;
    }

    private JTextField fileField1;
    private JTextField fileField2;
    private JTextField fileField3;
    private JTextField fileField4;

    public DiffActionListener(JTextField fileField1,JTextField fileField2,JTextField fileField3,JTextField fileField4){
        this.fileField1=fileField1;
        this.fileField2=fileField2;
        this.fileField3=fileField3;
        this.fileField4=fileField4;
    }
    public void actionPerformed(ActionEvent e) {
        String fileName1 = fileField1.getText();
        String fileName2 = fileField2.getText();
        String fileName3 = fileField3.getText();
        String column = fileField4.getText();
        if(StringUtils.isEmpty(fileName1)||
                StringUtils.isEmpty(fileName2)||
                StringUtils.isEmpty(fileName3)||
                StringUtils.isEmpty(column)){
            JOptionPane.showMessageDialog(null, "Error", "参数不能为空",JOptionPane.WARNING_MESSAGE);
            return;
        }
        File file1 = new File(fileName1);
        if(!file1.exists()){
            JOptionPane.showMessageDialog(null, "Error", "输入文件不存在"+fileName1,JOptionPane.WARNING_MESSAGE);
            return;
        }
        File file2 = new File(fileName2);
        if(!file2.exists()){
            JOptionPane.showMessageDialog(null, "Error", "输入文件不存在"+fileName2,JOptionPane.WARNING_MESSAGE);
            return;
        }
        File file3 = new File(fileName3);
        if(!file3.exists()){
            JOptionPane.showMessageDialog(null, "Error", "输出文件目录不存在"+fileName3,JOptionPane.WARNING_MESSAGE);
            return;
        }
        File outFile = new File(file3, "xp_"+file1.getName());
        if(!outFile.exists()){
            try {
                outFile.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
                JOptionPane.showMessageDialog(null, "Error", e1.getMessage(),JOptionPane.WARNING_MESSAGE);
                return;
            }
        }
        try {
            Map title = new HashMap();
            List<JSONObject> dataList = FinanceService.exportDiffExcel(column, file1, file2, title);
            OutputStream ou = new FileOutputStream(outFile);
            ExcelPrase.exportExcelFile(ou,dataList,title);
            ou.close();
        } catch (IOException e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error", e1.getMessage(),JOptionPane.WARNING_MESSAGE);
        }
        JOptionPane.showMessageDialog(null, "Success", "处理完成",JOptionPane.WARNING_MESSAGE);
    }
}
